defmodule KomutanWeb.Router do
 use Plug.Router
 use Plug.Debugger
 use Eml.HTML
 
  plug Plug.Static, at: "/doc", from: "doc"
  plug Plug.Static, at: "/static/css", from: "priv/static/css"
  #plug(Plug.Logger, log: :debug)
  plug(:match)
  plug(:dispatch)

 
  get "/" do 
   conn
	  |> put_resp_header("content-type", "text/html; charset=utf-8")
      |> send_resp(200, "Milis Web Tabanlı Sistem Yönetim Paneli: Komutan")
  end
  
  get("/servislerE", do: send_resp(conn, 200, servis_list()))
  
  get("/yardim", do: send_resp(conn, 200, "yardım"))
  
  get "/merhaba" do
    conn
	|> put_resp_header("content-type", "text/html; charset=utf-8")
	|> Plug.Conn.send_file(200, "priv/static/html/b.html")
  end
  
  get "/sistem/:bilgi" do
    conn
	  |> put_resp_header("content-type", "text/html; charset=utf-8")
      |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{Sistem.bilgi}")
  end
  
  get "/sistemA" do
    conn
	  |> put_resp_header("content-type", "text/html; charset=utf-8")
      |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{Sistem.aktif_bilgi}")
  end
  
  get "/surecler" do
    conn
	  |> put_resp_header("content-type", "text/html; charset=utf-8")
      |> send_resp(200, "<meta http-equiv='refresh' content='4' /> <p> #{sistem_surec()}")
  end
  
  get "/servisler" do
	conn |> servisler_page
  end
  
  def redirect(conn,url) do
	html = Plug.HTML.html_escape(url)
	body = "<html><body>You are being <a href=\"#{html}\">redirected</a>.</body></html>"
	conn
	|> put_resp_header("location", url)
	|> Plug.Conn.send_resp(200, body)
  end
  
  def servisler_page(conn) do
    conn
	|> put_resp_header("content-type", "text/html; charset=utf-8")
    |> send_resp(200, servis_list() |> servisler_eml)
  end
  
  def servisler_eml(servisler) do
    html  do
    #meta ["http-equiv": "refresh", content: "10"]
    link rel: "stylesheet", href: "/static/css/bootstrap.min.css"
    h2 "servisler"
	div class: "servisler" do
	  table border: "1", class: "table-md table-dark table-hover" do
		thead class: "thead-dark" do
		  tr do 
			th "servis adı"
			th "durum"
		  end
		end
		tbody do
		  Enum.map(servisler, fn(x) -> 
		  tr do
			td a [href: "/servisler/" <> x], x 
			case Servis.status(x) do
			true -> td([class: "bg-success"], a([ href: "/servisler/dur/" <> x , class: "text-dark"], "aktif"))
			false -> td([class: "bg-danger"],  a([ href: "/servisler/basla/" <> x, class: "text-dark"], "pasif"))
			_ -> td [class: "bg-warning"], "hata"
			end
		  end
		  end)
		end
	  end
	end
	end |> Eml.compile
  end
  
  def mytd() do 
	
  end
  
  # Use a variable in the route
  get "/servisler/:service" do
    sb=Servis.info(service)
    conn
	|> put_resp_header("content-type", "text/html; charset=utf-8")
    |> send_resp(200, "#{service} #{sb} servisi bilgisi gösterilir")
  end
  
  get "/servisler/:oper/:servis" do
    case oper do
    "basla" -> 
		case Servis.status(servis) do
			true -> conn |> servisler_page
			false -> servis |> Servis.start
			_ -> "hata"
		end
    "dur" -> 
		case Servis.status(servis) do
			true -> servis |> Servis.stop
			false -> conn |> servisler_page
			_ -> "hata" 
		end
    _ ->   "hatalı" 
    end
    conn
	|> put_resp_header("content-type", "text/html; charset=utf-8")
    |> send_resp(200, "<meta http-equiv='refresh' content='1' /> <p> #{servis} servisine #{oper} işlemi uygulandı.")
  end
  
  match(_, do: send_resp(conn, 404, "404 Bulunamadı!"))
  
  def servis_oper(servis,oper) do
    oper_type= case oper do
      "basla" -> "start" 
      "dur"   -> "stop" 
      _       -> "hata" 
    end
    
    case System.cmd("servis",[servis,oper_type])  do
      {_,0} -> "tamam"
      {_,1} -> "hata"
    end
  end
  
  def servis_list() do
    #servisler=Enum.reduce(File.ls!("/etc/init.d/"), fn(x, acc) -> "<span>#{x}</span>" <> "<p>" <> "<span>#{acc}</span>" end)
    #["<html>",servisler,"<html>"]
    #|> Enum.join("\n")
    {islev, 0}= __ENV__.function
    betik= [Application.get_env(:komutan, :betikyol), islev] 
    |> Enum.join("/")
    case System.cmd("sh",[betik]) do
      {_, 127} -> []
      {list, 0} -> list |> String.split("\n", trim: true)
    end
  end
  
  def sistem_surec, do: :os.cmd('echo q | htop | aha --black --line-fix')
  
end
